<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['user'][$_SESSION['s_userType'.$cfgPack]]) 
      && $cfgFileNames['user'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
  $technicianIdDetailArray = array();
  $editArray['userName'] = ""; 
  $editArray['firstName'] = ""; 
	$editArray['lastName']  = ""; 
  $editArray['address']   = "";  
  $editArray['city']      = "";  
  $editArray['mobile1']   = "";  
  $editArray['mobile2']   = "";  
  $editArray['phone']     = "";  
  $editArray['email']     = "";  
  $editArray['image']     = ""; 
  $editArray['password']     = ""; 
  $isEdit = 0;
  $userDetailArray  = array();
  $branchArray      = array();
  $technicianArray  = array();
  $techArray        = array();
  $selectedval      = "";
  $userName         = "";
  $userType         = "";
  $branchArray      = "";
  $userId           = isset($_REQUEST['userId']) ? $_REQUEST['userId'] : 0;
  $userLastInsertId = 0;
  $j                = "";  
  $technicianId     = isset($_REQUEST['technicianId']) ? $_REQUEST['technicianId'] : 0;
  $technicianName   = "";
  
  if(isset($_POST['cancelBtn']))
  {
    header("Location:employee.php");
    exit();
  }
  // after post : start 
  if(isset($_POST['insertBtn']))
  {
    if((isset($_POST['userId']) && $_POST['userId'] == 0) && (isset($_POST['userName']) && $_POST['userName'] != ""))
    {
      $insertsuer = "INSERT INTO user (userName,password,userType)
                           VALUE('".$_POST['userName']."','".$_POST['password']."','".$_POST['userType']."')";
      $insertUserResult = mysql_query($insertsuer);
      $userLastInsertId = mysql_insert_id();
    }
    if(isset($_POST['firstName']) && $_POST['firstName'] != "")
    {
      if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg"))
          && ($_FILES["file"]["size"] < 2000000000))
      {
        if ($_FILES["file"]["error"] > 0)
        {
          "Return Code: " . $_FILES["file"]["error"] . "<br />";
        }
        else
        {
          "Upload: " . $_FILES["file"]["name"] . "<br />";
          "Type: " . $_FILES["file"]["type"] . "<br />";
          "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
          "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
  
          if (file_exists("upload/" . $_FILES["file"]["name"]))
          {
            echo $_FILES["file"]["name"] . " already exists. ";
          }
          else
          {
          	$filename  =  pathinfo($_FILES["file"]["name"]);
          	$extension = $filename['extension']; 
          	$j = "img_".rand(100,999).".".$extension;
            move_uploaded_file($_FILES["file"]["tmp_name"],
            "upload/".$j);
            "Stored in: " . "upload/".$j;
          }
        }
      }
      else
      {
        echo "Invalid file";
      }
      // upload Image Code Over
      
      if($technicianId > 0)
      {
        if($_FILES["file"]["name"] == "")
          $file = $_POST["imgFile"];
        else
          $file = $j;
        
        $selectuserId = "SELECT technicianId,userId
                           FROM technician
                          WHERE technicianId = '".$_REQUEST['technicianId']."'";
        $selectuserIdRes = mysql_query($selectuserId);
        if($userIdRow = mysql_fetch_array($selectuserIdRes))
        {    
        	$userId = $userIdRow['userId'];
        }
          
        $updateUser ="UPDATE user SET userName = '".$_POST['userName']."',
                                      password = '".$_POST['password']."',
                                      userType = '".$_POST['userType']."'
                                  WHERE userId = ".$userId;
        $updateUserRes = mysql_query($updateUser);
          
        $updateTechnician = "UPDATE technician SET firstName    = '".$_POST['firstName']."',
                                                   lastName     = '".$_POST['lastName']."',
                                                   address      = '".$_POST['address']."',
                                                   city         = '".$_POST['city']."',   
                                                   mobile1      = '".$_POST['mobile1']."',   
                                                   mobile2      = '".$_POST['mobile2']."',   
                                                   phone        = '".$_POST['phone']."',   
                                                   email        = '".$_POST['email']."',   
                                                   userType     = '".$_POST['userType']."',   
                                                   image        = '".$file."'   
                                             WHERE technicianId = '".$_REQUEST['technicianId']."'";
         $updateTechnicianRes  = mysql_Query($updateTechnician);
         header("location:employee.php");  
      }
      else
      {
        if($userLastInsertId != 0)
        {
          $userId = $_SESSION['s_activeUserId'.$cfgPack];
          $address = addslashes($_POST['address']);
          $insertTechnician = "INSERT INTO technician (userId,userType,firstName,lastName,address,city,mobile1,
                                                          mobile2,phone,email,image)
                                VALUE('".$userLastInsertId."','".$_POST['userType']."','".$_POST['firstName']."','".$_POST['lastName']."',
                                      '".$address."','".$_POST['city']."','".$_POST['mobile1']."',
                                      '".$_POST['mobile2']."','".$_POST['phone']."','".$_POST['email']."',
                                      '".$j."')";
          $insertTechnicianResult = mysql_query($insertTechnician);  
          header("location:employee.php");  
        }
        else
        {
          $userId = $_SESSION['s_activeUserId'.$cfgPack];
          $address = addslashes($_POST['address']);
          $insertTechnician = "INSERT INTO technician (userId,userType,firstName,lastName,address,city,mobile1,
                                                       mobile2,phone,email,image)
                                VALUE('".$userId."','".$_POST['userType']."','".$_POST['firstName']."','".$_POST['lastName']."',
                                      '".$address."','".$_POST['city']."','".$_POST['mobile1']."',
                                      '".$_POST['mobile2']."','".$_POST['phone']."','".$_POST['email']."',
                                      '".$j."')";
          $insertTechnicianResult = mysql_query($insertTechnician);  
          header("location:employee.php");   
        }
      }
    }
  }
 
//  // after post : end
  $branch = "SELECT branchId,branchName
               FROM branch";
  $branchResult = mysql_query($branch);
  $a = 0;  
  while ($branchRow = mysql_fetch_array($branchResult))
  {
  	$branchArray['branchId'][$a]   = $branchRow['branchId'];
  	$branchArray['branchName'][$a] = $branchRow['branchName'];
  	$a++;
  }
       
   $selectTechnician = "SELECT technician.technicianId,technician.userType,technician.firstName,technician.lastName,
                              technician.address,technician.city,technician.mobile1,technician.mobile2,
                              technician.phone,technician.email,technician.image,user.userName,technician.userId,user.password
                         FROM technician
                         LEFT JOIN user ON technician.userId = user.userId
                        WHERE technician.status = 'A'
                     ORDER BY technician.firstName";
  $selectTechnicianResult = mysql_query($selectTechnician);
  $i = 0;
  while($techRow = mysql_fetch_array($selectTechnicianResult))
  {    
    $technicianIdDetailArray[$i]['userName']        = $techRow['userName'];
    $technicianIdDetailArray[$i]['userType']        = $techRow['userType'];
    $technicianIdDetailArray[$i]['password']        = $techRow['password'];
    $technicianIdDetailArray[$i]['technicianId']    = $techRow['technicianId'];
    $technicianIdDetailArray[$i]['technicianName']  = $techRow['firstName']." ".$techRow['lastName'];
    $technicianIdDetailArray[$i]['address']         = $techRow['address'];
    $technicianIdDetailArray[$i]['city']            = $techRow['city'];
    $technicianIdDetailArray[$i]['mobile1']         = $techRow['mobile1'];
    $technicianIdDetailArray[$i]['mobile2']         = $techRow['mobile2'];
    $technicianIdDetailArray[$i]['phone']           = $techRow['phone'];
    $technicianIdDetailArray[$i]['email']           = $techRow['email'];
    $technicianIdDetailArray[$i]['image']           = $techRow['image'];
    if($techRow['technicianId'] == $technicianId)
    {
      $isEdit = 1;
      $userId                 = $techRow['userId']; 
      $editArray['userName']  = $techRow['userName']; 
      $editArray['userType']  = $techRow['userType']; 
      $editArray['firstName'] = $techRow['firstName']; 
    	$editArray['lastName']  = $techRow['lastName']; 
      $editArray['address']   = $techRow['address'];  
      $editArray['city']      = $techRow['city'];  
      $editArray['mobile1']   = $techRow['mobile1'];  
      $editArray['mobile2']   = $techRow['mobile2'];  
      $editArray['phone']     = $techRow['phone'];  
      $editArray['email']     = $techRow['email'];  
      $editArray['image']     = $techRow['image'];  
      $editArray['password']  = $techRow['password'];  
    }
    $i++;
  }
  
  $technicianForFilter = "SELECT DISTINCT userType
                            FROM technician";
  $techRes = mysql_query($technicianForFilter);
  $c = 0;  
  while ($techRow = mysql_fetch_array($techRes))
  {
  	 $techArray['userType'][$c] = $techRow['userType'];
  	 $c++;
  } 
  include("./bottom.php");
  $smarty->assign("technicianId",$technicianId);
  $smarty->assign("userId",$userId);
  $smarty->assign("userName",$userName);
  $smarty->assign("userType",$userType);
  $smarty->assign("branchArray",$branchArray);
  $smarty->assign("userDetailArray",$userDetailArray);
  $smarty->assign("technicianId",$technicianId);
  $smarty->assign("technicianName",$technicianName);
  $smarty->assign("technicianIdDetailArray",$technicianIdDetailArray);
  $smarty->assign("isEdit",$isEdit);
  $smarty->assign("editArray",$editArray);
  $smarty->assign("technicianArray",$technicianArray);
  $smarty->assign("techArray",$techArray);
  $smarty->assign("selectedval",$selectedval);
  $smarty->display("employee.tpl");
}
?>